{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\")\n",
    "print(type(world_alcohol))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10 15 20]\n",
      "[[ 5 10 15]\n",
      " [20 25 30]\n",
      " [35 40 45]]\n"
     ]
    }
   ],
   "source": [
    "#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "#When we input a list of lists, we get a matrix as a result:\n",
    "matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])\n",
    "print vector\n",
    "print matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "#We can use the ndarray.shape property to figure out how many elements are in the array\n",
    "vector = numpy.array([1, 2, 3, 4])\n",
    "print(vector.shape)\n",
    "#For matrices, the shape property contains a tuple with 2 elements.\n",
    "matrix = numpy.array([[5, 10, 15], [20, 25, 30]])\n",
    "print(matrix.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Each value in a NumPy array has to have the same data type\n",
    "#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. \n",
    "#You can check the data type of a NumPy array using the dtype property.\n",
    "numbers = numpy.array([1, 2, 3, 4])\n",
    "numbers.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[             nan,              nan,              nan,\n",
       "                     nan,              nan],\n",
       "       [  1.98600000e+03,              nan,              nan,\n",
       "                     nan,   0.00000000e+00],\n",
       "       [  1.98600000e+03,              nan,              nan,\n",
       "                     nan,   5.00000000e-01],\n",
       "       ..., \n",
       "       [  1.98700000e+03,              nan,              nan,\n",
       "                     nan,   7.50000000e-01],\n",
       "       [  1.98900000e+03,              nan,              nan,\n",
       "                     nan,   1.50000000e+00],\n",
       "       [  1.98500000e+03,              nan,              nan,\n",
       "                     nan,   3.10000000e-01]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#When NumPy can't convert a value to a numeric data type like float or integer, it uses a special nan value that stands for Not a Number\n",
    "#nan is the missing data\n",
    "#1.98600000e+03 is actually 1.986 * 10 ^ 3\n",
    "world_alcohol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[u'1986' u'Western Pacific' u'Viet Nam' u'Wine' u'0']\n",
      " [u'1986' u'Americas' u'Uruguay' u'Other' u'0.5']\n",
      " [u'1985' u'Africa' u\"Cte d'Ivoire\" u'Wine' u'1.62']\n",
      " ..., \n",
      " [u'1987' u'Africa' u'Malawi' u'Other' u'0.75']\n",
      " [u'1989' u'Americas' u'Bahamas' u'Wine' u'1.5']\n",
      " [u'1985' u'Africa' u'Malawi' u'Spirits' u'0.31']]\n"
     ]
    }
   ],
   "source": [
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\", dtype=\"U75\", skip_header=1)\n",
    "print(world_alcohol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "Cte d'Ivoire\n"
     ]
    }
   ],
   "source": [
    "uruguay_other_1986 = world_alcohol[1,4]\n",
    "third_country = world_alcohol[2,2]\n",
    "print uruguay_other_1986\n",
    "print third_country"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10 15]\n"
     ]
    }
   ],
   "source": [
    "vector = numpy.array([5, 10, 15, 20])\n",
    "print(vector[0:3])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 25 40]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 10]\n",
      " [20 25]\n",
      " [35 40]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[:,0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[20 25]\n",
      " [35 40]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[1:3,0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
